import {
  Controller,
  Post,
  Body,
  Get,
  Param,
  Delete,
  BadRequestException,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ProductService } from './product.service';
import { CreateProductDto } from './dto/product.dto';
import { Product } from './entities/product.entity'; // 假设您有一个Product实体
import { PaginationDto, PaginatedResponseDto } from '../common/dto/pagination.dto';
import { ApiOperation, ApiResponse, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { UserRole } from '../auth/enums/user.enum';

@ApiTags('产品')
@Controller('products')
export class ProductController {
  constructor(private readonly productService: ProductService) {}

  @Post()
  @ApiOperation({ summary: '创建产品', description: '创建新的产品' })
  @ApiResponse({ status: 201, description: '创建成功', type: Product })
  create(@Body() createProductDto: CreateProductDto): Promise<Product> {
    return this.productService.create(createProductDto);
  }

  @Get()
  @ApiOperation({ summary: '获取所有产品', description: '获取所有产品（不分页）' })
  @ApiResponse({ status: 200, description: '获取成功', type: [Product] })
  findAll(): Promise<Product[]> {
    return this.productService.findAll();
  }

  @Get('paginated')
  @ApiOperation({ summary: '分页获取产品', description: '分页获取所有产品' })
  @ApiResponse({ status: 200, description: '获取成功', type: PaginatedResponseDto })
  findAllPaginated(@Query() paginationDto: PaginationDto): Promise<PaginatedResponseDto<Product>> {
    return this.productService.findAllPaginated(paginationDto);
  }

  @Get('recommended')
  @ApiOperation({ summary: '获取推荐产品', description: '获取所有推荐产品（不分页）' })
  @ApiResponse({ status: 200, description: '获取成功', type: [Product] })
  findRecommendedProducts(): Promise<Product[]> {
    return this.productService.findRecommendedProducts(); // 获取推荐产品
  }

  @Get('recommended/paginated')
  @ApiOperation({ summary: '分页获取推荐产品', description: '分页获取所有推荐产品' })
  @ApiResponse({ status: 200, description: '获取成功', type: PaginatedResponseDto })
  findRecommendedProductsPaginated(@Query() paginationDto: PaginationDto): Promise<PaginatedResponseDto<Product>> {
    return this.productService.findRecommendedProductsPaginated(paginationDto);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取单个产品', description: '根据ID获取产品' })
  @ApiResponse({ status: 200, description: '获取成功', type: Product })
  @ApiResponse({ status: 400, description: '无效的产品ID' })
  @ApiResponse({ status: 404, description: '产品不存在' })
  findOne(@Param('id') id: string): Promise<Product> {
    const productId = Number(id); // 将id转换为数字
    if (isNaN(productId)) {
      throw new BadRequestException('Invalid product ID'); // 如果转换失败，抛出异常
    }
    return this.productService.findOne(productId);
  }

  @Delete(':id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: '删除产品', description: '根据ID删除产品' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 400, description: '无效的产品ID' })
  @ApiResponse({ status: 404, description: '产品不存在' })
  async remove(@Param('id') id: string): Promise<{ message: string }> {
    const productId = Number(id);
    if (isNaN(productId)) {
      throw new BadRequestException('Invalid product ID');
    }
    await this.productService.remove(productId);
    return { message: `Product with ID ${productId} has been successfully deleted` };
  }
}
